package com.ruoyi.web.controller.system.factory;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 系统 登录 工厂
 *
 * @author Jyl
 */
@Log4j2
@Component
public class SysLoginFactory extends JoController
{

    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysUserService userService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    public AjaxResult login(LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        Map map = loginService.login(
                loginBody.getUsername(),
                loginBody.getPassword(),
                loginBody.getCode(),
                loginBody.getUuid(),
                loginBody.getUdid()
        );
        ajax.put(Constants.TOKEN, MapUtil.getStr(map, "token"));

        //用户id
        String userId = MapUtil.getStr(map, "userId");
        //通知flyflow登录成功
        String flyflowBizUrl = SpringUtil.getProperty("flyflow.url");
        String url = StrUtil.format("{}login/auto", flyflowBizUrl);

        String post = HttpUtil.post(url, JSON.toJSONString(map));

        JSONObject jsonObject = JSON.parseObject(post);
        Boolean ok = jsonObject.getBoolean("ok");
        if (ok)
        {
            return ajax;
        }

        return AjaxResult.error("登录失败");
    }

    /**
     * App登录
     *
     * @param loginBody 登录参数
     * @return 返回登录凭证
     */
    public AjaxResult appLogin(LoginBody loginBody)
    {
        //覆写登录验证码
        loginBody.setCode(Constants.CUSTOMER_TYPE_1001);
        loginBody.setUuid(loginService.devToken(loginBody.getCode()));
        return login(loginBody);
    }

    /**
     * 重置密码
     *
     * @param loginBody 登录参数
     * @return 返回登录凭证
     */
    public AjaxResult resetPwd(LoginBody loginBody)
    {
        if (loginService.validateCaptcha(loginBody.getCode(), loginBody.getUuid()))
        {
            SysUser user = userService.selectUserByUserName(loginBody.getUsername());
            if (user != null && StrUtil.isNotEmpty(user.getUserName()))
            {
                SysUser newUser = new SysUser();
                newUser.setUserId(user.getUserId());
                newUser.setPassword(SecurityUtils.encryptPassword(loginBody.getPassword()));
                newUser.setUpdateBy(user.getUserName());
                userService.checkUserAllowed(newUser);
                return toAjax(userService.resetPwd(newUser));
            }
            return JoR.error("重置失败");
        }
        return JoR.error("重置失败");
    }

}
